package com.nail.api.customMake.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nail.api.customMake.bean.Address;
import com.nail.api.customMake.bean.Country;
import com.nail.api.customMake.dao.IAddressDao;
import com.nail.api.customMake.dao.ICountryDao;
import com.nail.api.customMake.service.IAddressService;

@Service("addressService")
public class AddressServiceImpl implements IAddressService{

	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private ICountryDao countryDao;
	
    @Autowired
    private IAddressDao addressDao;

    @Transactional
    public void addAddress(Address address){
        addressDao.insertSelective(address);
    }

    @Transactional
    public void deleteAddress(Long userId,Long addressId){
        if(userId == null){
            addressDao.deleteByPrimaryKey(addressId);
        }else{
            addressDao.deleteByUserIdAndId(addressId,userId);
        }
    }

    @Transactional
    public void updateAddress(Address address){
        addressDao.updateByPrimaryKeySelective(address);
    }

    public List<Address> getAddress(Long userId, Long SelectorId,Long addressId){
        //过滤非管理员账户的查询
        if(SelectorId != null && !userId.equals(SelectorId)){
            return null;
        }
        List<Address> list;
        if(addressId != null && userId != null){
            list = addressDao.getAddressByUserIdAndAddressId(userId,addressId);
        }else{
            list = addressDao.getAddressByUserId(userId);
        }
        return list;
    }

    public List<Country> getCountry(){
    	return countryDao.selectAll();
    }
}
